﻿using AutoMapper;
using Fake_Shop_WebApi.Dtos;
using Fake_Shop_WebApi.Models;
using Fake_Shop_WebApi.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Fake_Shop_WebApi.Controllers
{
    [Route("api/TouristRoutes/{tourisId}/picture")] //pictureId   TouristRoutePicture
    [ApiController]
    public class TouristRoutePictureControllers:ControllerBase
    {
        private ITouristRouteRepository _touristRouteRepository;
        private readonly IMapper _mapper;
        public TouristRoutePictureControllers(ITouristRouteRepository touristRouteRepository, IMapper mapper)
        {
            _touristRouteRepository = touristRouteRepository;
            _mapper = mapper;
        }
        [HttpGet]
        public async Task<IActionResult> GetTouristRoutePicture(Guid tourisId)
        {
            if (!await _touristRouteRepository.GetTouristRouteExistsAsync (tourisId))
            {
                return NotFound("旅游路线不存在");
            }
            var touristRoutePicture =  await  _touristRouteRepository.GetTouristRoutePicturesAsync(tourisId);
            if(touristRoutePicture==null || touristRoutePicture.Count() <= 0)
            {
                return NotFound("旅游图片不存在");
            }
            return Ok(_mapper.Map<IEnumerable<TouristRoutePictureDto>>(touristRoutePicture));
        }
        [HttpGet("{pictureId}",Name = "GetTouristRoutePicture")]
        public async Task<IActionResult> GetTouristRoutePicture(Guid tourisId,int pictureId)
        {
            if (!await _touristRouteRepository.GetTouristRouteExistsAsync(tourisId))
            {
                return NotFound("旅游线路不存在");
            }
            var touristRoutePicturePro = await  _touristRouteRepository.GetPictureAsync(pictureId);
            if(touristRoutePicturePro == null)
            {
                return NotFound("图片不存在");
            }
            return Ok(_mapper.Map<TouristRoutePicture>(touristRoutePicturePro));
        }
        [HttpPost]
        [Authorize(AuthenticationSchemes = "Bearer")]
        public async Task< IActionResult > CrateTouristRoutePicture([FromRoute]Guid touristRoutePicturId,[FromBody]TouristRoutePictureForCarionDto routePictureForCarionDto)
        {
            if (! await _touristRouteRepository.GetTouristRouteExistsAsync(touristRoutePicturId))
            {
                return NotFound("找不到旅游路线");
            }
            var pictureModel = _mapper.Map<TouristRoutePicture>(routePictureForCarionDto);
            _touristRouteRepository.AddTouristRoutePictures(touristRoutePicturId, pictureModel);
            await _touristRouteRepository.SaveAsync();
            var touristRoutePro = _mapper.Map<TouristRoutePictureDto>(pictureModel);
            return CreatedAtRoute("GetTouristRoutePicture", new {
                tourisId = pictureModel.TouristRouteId,
                pictureId = pictureModel.Id
            }, touristRoutePro);
        }
        [HttpDelete("{pictureId}")]
        [Authorize(AuthenticationSchemes = "Bearer")]
        public async  Task<IActionResult> DeleteTouristRoutePicture([FromRoute] Guid touristRouteId, int pictureId) 
        {
            if (!await _touristRouteRepository.GetTouristRouteExistsAsync(touristRouteId))
            {
                return NotFound("找不到旅游图片");
            }
            var touristRoute = await _touristRouteRepository.GetPictureAsync(pictureId);
            _touristRouteRepository.DeleteTouristRoutePictures(touristRoute);
            await _touristRouteRepository.SaveAsync();
            return NoContent();
        }

    }
}
